package hzjbbis.framework.workflow.action;

import hzjbbis.framework.context.ContextFacade;
import hzjbbis.framework.user.UserInfo;
import hzjbbis.framework.workflow.service.WorkflowService;
import hzjbbis.framework.workflow.util.CurrentUser;
import hzjbbis.framework.workflow.util.Global;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class WorkflowAction extends Action {
    private WorkflowService getWorkflowService() {
        return (WorkflowService) Global.getServiceLocator().getService(
            "workflowService");
    }

    public ActionForward execute(
        ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) {
        
        String formId = request.getParameter("FormId");
        String path = "/";
        
        if (formId != null && NumberUtils.isDigits(formId)) {
            UserInfo user = ContextFacade.getUserContext(request).getUserInfo();
            CurrentUser.set(user);

            path = getWorkflowService().getCurrentAction(Long.parseLong(formId));
            
            if (path == null)
                path = "/";
            
            if (path.indexOf("/") == -1)
                path = "/" + path;
        }
        
        return (new ActionForward(path));
    }
}
